package customer.zspt.cn.customer.components.contact;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.bean.FriendInfoDao;
import customer.zspt.cn.customer.components.chat.FriendChat;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.utils.ChatRecordSPUtils;
import customer.zspt.cn.customer.utils.GetAllInfoUtils;
import customer.zspt.cn.customer.utils.SPUtils;
public class contact_view extends Activity implements View.OnClickListener {
    //用户头像
    @ViewInject(R.id.head_portrait)
    public ImageView head_portrait;
    //用户真实名字
    @ViewInject(R.id.userCAName)
    public TextView userCAName;
    //y用户昵称
    @ViewInject(R.id.userEGName)
    public TextView userEGName;
    @ViewInject(R.id.clearbutton)
    public Button clearbutton;
    private FriendInfoDao.FriendInfo data;
    @ViewInject(R.id.loading)
    private View loading;
    @ViewInject(R.id.send)
    private Button send;
    @ViewInject(R.id.tag_tv)
    private TextView tag_tv;
    @ViewInject(R.id.back)
    private ImageView back;
    @ViewInject(R.id.UserName)
    private TextView UserName;
    private String kefuId;
    private String token;
    private String friendInfoStr;
    private Gson gson;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            String data = (String) msg.obj;
            FriendInfoDao dao = gson.fromJson(data,FriendInfoDao.class);
            toPull(gson.toJson(dao.data));
        }
    };
    private String friendInfo;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_view);
        ViewUtils.inject(this);
        gson = new Gson();
        UserName.setText("资料页");
        back.setOnClickListener(this);
        Intent intent = getIntent();
        kefuId = intent.getStringExtra("kefuId");
        Log.e("kefuId",kefuId);
        token = SPUtils.getString(this, "token", "");
        friendInfo = SPUtils.getString(this, constant.FRIEND_INFO + kefuId, "");
        toPull(friendInfo);
        getUserInfo();
    }
    private void initUI(FriendInfoDao.FriendInfo data) {
        friendInfoStr = friendInfo;
        BitmapUtils utils = new BitmapUtils(this);
        utils.display(head_portrait, data.kefuAvatar);
        userCAName.setText(data.kefuRealname);
        userEGName.setText(data.kefuUsername);
        String tags = " ";
        if (data.skills != null) {
            if (data.skills.size() > 0) {
                for (int i = 0; i < data.skills.size(); i++) {
                    tags += data.skills.get(i).skillTitle + " ";
                }
            }
        }
        tag_tv.setText(tags);
        send = (Button) findViewById(R.id.send);
        send.setOnClickListener(this);
        clearbutton.setOnClickListener(this);
    }
    private void toPull(String friendInfo) {
        try {
            FriendInfoDao.FriendInfo friendInfoDao = gson.fromJson(friendInfo, FriendInfoDao.FriendInfo.class);
            kefuId = "companyid_" + friendInfoDao.companyid + "_usertype_kefu"
                    + "_user_" + friendInfoDao.kefuId;
            initUI(friendInfoDao);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send:
                Intent intent = new Intent(contact_view.this, FriendChat.class);
                intent.putExtra("friendInfo", friendInfoStr);
                intent.putExtra("type", "0");
                startActivity(intent);
                finish();
                break;
            case R.id.clearbutton:
                if (kefuId != null) {
                    showTipDialog();
                }
                break;
            case R.id.back:
                finish();
                break;
        }
    }
    private void showTipDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提醒");
        builder.setMessage("清空不可恢复，是否继续！！！");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ChatRecordSPUtils.setRecordString(getApplication(), kefuId, kefuId, "");
                showLoadingDialog();
            }
        });
        builder.setNegativeButton("取消",null);
        builder.show();
    }
    private void showLoadingDialog()
    {
           loading.setVisibility(View.VISIBLE);
           new Handler().postDelayed(new Runnable() {
               @Override
               public void run() {
                   loading.setVisibility(View.GONE);
                   Toast.makeText(getApplication(),"清除成功",Toast.LENGTH_SHORT).show();
               }
           },1000);
    }
   /**获取用户详细资料*/
    public void getUserInfo() {
        GetAllInfoUtils.infoUtils.getFriendInfo(constant.AllMSG + "/kefu/info/" + kefuId,
                token, this);
        GetAllInfoUtils.infoUtils.monitoringLinster(new GetAllInfoUtils.ReadyData() {
            @Override
            public void success(String data) {
                Message msg = new Message();
                msg.obj = data;
                mHandler.sendMessage(msg);
            }
            @Override
            public void fail() {

            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }
}
